<?php

	class source
	{

		var $fw		= null;
		var $sec	= null;

		function source(&$framework, &$security)
		{

			$this->fw	= &$framework;
			$this->sec	= &$security;

			$this->fw->js[] = 'prefrences.js';

			switch($this->fw->do)
			{

				case 'count':
					$this->process_count();
					break;

				case 'process':
					$this->process_prefrences_form();
					break;

				default:
					$this->show_prefrences_form();
					break;

			}

		}

		// update this members movie_count
		function process_count()
		{
			$member_id	= (int)$this->sec->dtls['id'];

			// get the number of movies
			$sql	= $this->fw->_pq("SELECT COUNT(DISTINCT `movie_id`) FROM `#__own` WHERE `member_id` = %s", $member_id);
			$result	= mysql_query($sql) or die(mysql_error());
			$num	= mysql_result($result, 0, 0);

			// update the number of movies
			$sql	= $this->fw->_pq("UPDATE `#__member` SET `updated` = UNIX_TIMESTAMP(), `movie_count` = %s WHERE `member_id` = %s", (int)$num, $member_id);
			$result	= mysql_query($sql) or die(mysql_error());

			$_SESSION['msg'] = 'Movie count updated to '. $num;
			header('location: index.php?what=prefrences');
			exit;
		}

		// Process the prefrences form
		function process_prefrences_form()
		{

			$member_id			= (int)$this->sec->dtls['id'];

			$username			= trim($_POST['username']);
			$email_address		= trim($_POST['email_address']);
			$password			= $_POST['password'];
			$password_confirm	= $_POST['password_confirm'];

			$public_collection	= (int)$_POST['public_collection'];
			$imdb_open_window	= (int)$_POST['imdb_open_window'];

			// Error Checking
			$err = array();

			if(empty($username))
			{

				$err['username'] = 'Field required';

			}
			elseif(strlen($username) < 3)
			{

				$err['username'] = 'Field requires at least 3 characters';

			}

			if(empty($email_address))
			{

				$err['email_address'] = 'Field required';

			}
			elseif(!$this->fw->is_email($email_address))
			{
				$err['email_address'] = 'Invalid Email Address';
			}

			if(!empty($password))
			{

				if(strlen($password) < 3)
				{
					$err['password'] = 'Password must be at least 3 characters';
				}
				elseif($password != $password_confirm)
				{
					$err['password_confirm'] = 'Password does not match';
				}

			}

			if(count($err) == 0)
			{

				// error checking completed, save the changes!
				$password	= (!empty($password) ? ", member_password = MD5('". addslashes($password) ."')" : null);
				$sql	= $this->fw->_pq("UPDATE #__member SET member_username = '%s', member_email = '%s', pref_public_collection = %s, pref_imdb_window = %s ". $password ." WHERE member_id = %s", $username, $email_address, $public_collection, $imdb_open_window, $member_id);
				$result	= mysql_query($sql) or die(mysql_error());

				$_SESSION['msg'] = 'Prefrences Successfully Updated';
				header('location: index.php?what=prefrences');
				exit;

			}
			else
			{

				// An error has been found!
				$this->show_prefrences_form('An error has been found!', $err);

			}

		}

		// --= This shows a form that will allow the current member to update their username, email, password & prefrences
		function show_prefrences_form($err_msg = null, $err = array())
		{

			// Paper Chain
			$this->fw->pc_item('Prefrences &amp; Account Details', 'Update your account details &amp; prefrences to enhance your movie manager experience', 'index.php?what=prefrences');

			$member_id = (int)$this->sec->dtls['id'];



			if(isset($_REQUEST['what']) && $_REQUEST['what'] == 'process')
			{

				// Obtain data from post
				$username			= '';
				$email				= '';

				$public_collection	= (int)$_POST['public_collection'];
				$iow	= (int)$_POST['imdb_open_window'];

			}
			else
			{

				// Obtain data from the database
				$member = $this->fw->get_member_details($member_id);

				$username			= $member['username'];
				$email				= $member['email'];

				$iow				= $member['preference']['imdb_open_window'];
				$public_collection	= $member['preference']['public_collection'];

			}

			// Template Code
			$this->fw->template	= 'prefrences.form.tpl';

			// Form Data
			$this->fw->content['username']			= htmlentities($username);
			$this->fw->content['email_address']		= htmlentities($email);
			$this->fw->content['imdb_open_window']	= $iow;
			$this->fw->content['public_collection']	= $public_collection;

			// Form Options
			// imdb_open_window options
			$this->fw->content['public']	= array(0 => 'Viewable By Me Only!',
													1 => 'Other Registered Members',
													2 => 'Available Publically');

			$this->fw->content['iow']		= array(0 => 'Current Window',
													1 => 'New Window');

			// Error Variables

		}

	}

?>